package com.ruoyi.collection.process;

import com.alibaba.fastjson.JSON;
import com.ruoyi.bizsys.domain.CustBankCard;
import com.ruoyi.bizsys.domain.RealNameAuth;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.ICustBankCardService;
import com.ruoyi.bizsys.service.IRealNameAuthService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.QueryRecentOrderReq;
import com.ruoyi.collection.domain.QueryRecentOrderResp;
import com.ruoyi.collection.mapper.BCollectionMapper;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.util.business.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 还款流水查询(3天)
 * @author Victor
 *
 */
@Slf4j
@Service("QueryRecentOrder")
public class QueryRecentOrderPorcessor implements CollectionProcess {
	
	@Autowired
	private ICustBankCardService custBankCardService;
	
	@Autowired
    private IRealNameAuthService realNameAuthService;

	@Autowired
	private BCollectionMapper collectionMapper;

	@Override
	public Object process(ApiParameter req) throws Exception {
		
		log.info("还款流水查询开始");

		QueryRecentOrderReq reqBody = JSON.parseObject(req.getBody().toString(), QueryRecentOrderReq.class);
		QueryRecentOrderResp respBody = new QueryRecentOrderResp();
		List<Map<String, Object>> paymentList = new ArrayList<Map<String, Object>>();
		respBody.setPaymentList(paymentList);
		
		// 获取所有custNo
		RealNameAuth realNameAuthCondition = new RealNameAuth();
		realNameAuthCondition.setBvnAccount(reqBody.getCustId());
		realNameAuthCondition.setState(Constants.BIZ_STATUS_1);
		List<RealNameAuth> realNameAuthList = realNameAuthService.selectRealNameAuthList(realNameAuthCondition);
		if (CollectionUtils.isEmpty(realNameAuthList)) {
			log.info("无实名信息,查询逾期罚息结束");
			return respBody;
		}
		List<String> custNos = new ArrayList<String>();
		log.info("step1:");
		// 查询3天订单
		for(RealNameAuth realNameAuth : realNameAuthList){
			log.info("step2:"+realNameAuth.getCustNo());
			if(custNos.contains(realNameAuth.getCustNo())){
				continue;
			}
			custNos.add(realNameAuth.getCustNo());
			RepaymentOrder orderCondition = new RepaymentOrder();
			orderCondition.setCustNo(realNameAuth.getCustNo());
			orderCondition.setCreateTime(DateUtils.addDays(new Date(), -3));
			orderCondition.setState(UserConstants.BUSINESS_NORMAL);
			log.info("step3:"+collectionMapper);
			List<RepaymentOrder> repaymentOrders = collectionMapper.selectRepaymentOrderList(orderCondition);
			log.info("step4:"+repaymentOrders.toString());
			if(CollectionUtils.isNotEmpty(repaymentOrders)){
				for(RepaymentOrder repaymentOrder : repaymentOrders){
					Map<String, Object> payment = new HashMap<String, Object>();
					
					// 查询银行卡
					CustBankCard custBankCardCondition = new CustBankCard();
					custBankCardCondition.setCustNo(realNameAuth.getCustNo());
					custBankCardCondition.setAuthorizationCode(repaymentOrder.getBankCard());
					custBankCardCondition.setState(UserConstants.BUSINESS_NORMAL);
					List<CustBankCard> custBankCards = custBankCardService.selectCustBankCardList(custBankCardCondition);
					
					CustBankCard custBankCard = null;
					if(CollectionUtils.isNotEmpty(custBankCards)){
						custBankCard = custBankCards.get(0);
					}
					
					payment.put("orderNo", repaymentOrder.getLoanSerialNo());
					payment.put("cardNo", null != custBankCard ? custBankCard.getCardNumber() : null);
					payment.put("cardType", null != custBankCard ? custBankCard.getCardType() : null);
					payment.put("bankName", null != custBankCard ? custBankCard.getBankName() : null);
					payment.put("txnAmount", repaymentOrder.getRepaymentAmt());
					payment.put("txnType", repaymentOrder.getActionType());
					payment.put("resultMessage", repaymentOrder.getRemark());
					payment.put("updateDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, repaymentOrder.getUpdateTime()));
					payment.put("payTime", repaymentOrder.getPayTime()==null?"":DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, repaymentOrder.getPayTime()));
					
					if(BizConstants.REPAYMENT_ORDER_STATUS_SUCCESS == repaymentOrder.getRepayStatus()){
						payment.put("orderStatus", "S");
					}else if(BizConstants.REPAYMENT_ORDER_STATUS_DOING == repaymentOrder.getRepayStatus()){
						payment.put("orderStatus", "P");
					}else if(BizConstants.REPAYMENT_ORDER_STATUS_WAIT == repaymentOrder.getRepayStatus()){
						payment.put("orderStatus", "I");
					}else{
						payment.put("orderStatus", "F");
					}
					
					paymentList.add(payment);
				}
			}
		}
		
		
		log.info("还款流水查询结束");
		
		return respBody;
		
	}

}
